<?php

namespace app\common\model\rate;

use app\common\utils\Utils;
use think\exception\ValidateException;
use think\Model;
use traits\model\SoftDelete;

class Company extends Model
{

    use SoftDelete;
    // 设置当前模型的数据库连接
    protected $connection = [
        // 数据库类型
        'type'        => 'mysql',
        // 服务器地址
        'hostname'    => '47.104.65.211',
        // 数据库名
        'database'    => 'jiuyetongzuixin',
        // 数据库用户名
        'username'    => 'jiuyetongzuixin',
        // 数据库密码
        'password'    => '123456',
        // 数据库编码默认采用utf8
        'charset'     => 'utf8',
        // 数据库表前缀
        'prefix'      => 'ss_',
        // 数据库调试模式
        'debug'       => false,
    ];


    // 表名
    protected $name = 'company';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'management_text',
        'register_time_text',
        'service_type_text',
        'is_jieti_text'
    ];

    protected static function init()
    {


    }

    public function getIdentificationswitchList()
    {
        return ['1' => '已认证', '0' => '未认证'];
    }

    public function getManagementList()
    {
        return ['1' => __('Management 1'), '0' => __('Management 0')];
    }

    public function getServiceTypeList()
    {
        return ['0' => __('Service_type 0'), '1' => __('Service_type 1')];
    }

    public function getIsJietiList()
    {
        return ['0' => __('Is_jieti 0'), '1' => __('Is_jieti 1')];
    }


    public function getManagementTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['management']) ? $data['management'] : '');
        $list = $this->getManagementList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRegisterTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['register_time']) ? $data['register_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getServiceTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['service_type']) ? $data['service_type'] : '');
        $list = $this->getServiceTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsJietiTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_jieti']) ? $data['is_jieti'] : '');
        $list = $this->getIsJietiList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setRegisterTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function rate()
    {
        return $this->belongsTo('app\common\model\rate\Area', 'rate_area_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
